﻿Imports System.Globalization
Imports System.Data.SqlClient
Public Class Profile_mobile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Enable or disable edit profile button and guide button , if member is the profile owner or not.
        If Request.Params("Bibliophile") = User.Identity.Name Then
            'btn_EditProfile.Visible = True
            btn_DeleteGuide.Visible = True

            hl_Pending.Enabled = True
            hl_Pending.NavigateUrl = "~/BooksPendingByBibliophile.aspx?Bibliophile=" + Request.Params("Bibliophile")
        Else
            'btn_EditProfile.Visible = False
            btn_DeleteGuide.Visible = False

            hl_Pending.Enabled = False
        End If
        If Request.Params("Bibliophile") = "" Then
            Response.Redirect("~/Account/Login.aspx")
        End If
        'Set the name and the role of the member.
        lbl_UserName.Text = Request.Params("Bibliophile")
        lbl_RoleName.Text = String.Join("", System.Web.Security.Roles.GetRolesForUser(Request.Params("Bibliophile")))

        'Populate the messages
        Comments.GetMessages(Request.Params("Bibliophile"), CommentsLtr)

        'Check if owner of profile has any favorite book
        Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("BooksReviewDBConnectionString1").ConnectionString.ToString)
        Dim SqlString As String = "SELECT * FROM FavoriteBook_WithName WHERE Bibliophile = @f1"
        Dim cmd As SqlCommand = New SqlCommand(SqlString, conn)
        conn.Open()
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", Request.Params("Bibliophile"))

        'Getting records (only 1 line for selected Bibliophile)
        Dim dr As SqlDataReader = cmd.ExecuteReader()
        If dr.HasRows Then
            dr.Read()
            If Not IsDBNull(dr("Book_FK")) Then
                lbl_NoFavBook.Visible = False
                lbl_FavBook.Text = dr("BookName")
                lbl_FavBook.Visible = True
                rpt_FavBook.Visible = True
            End If
        End If
        dr.Close()

        Dim cmdPendingBooks As New SqlCommand("SELECT * FROM [TotalPostsPerBibliophile] WHERE ([Bibliophile] = @f1 AND [IsVerified] = 0)", conn)
        cmdPendingBooks.Parameters.AddWithValue("@f1", Request.Params("Bibliophile"))

        'Getting records (only 1 line for selected Bibliophile)
        Dim drPendingBooks As SqlDataReader = cmdPendingBooks.ExecuteReader()
        If drPendingBooks.HasRows Then
            drPendingBooks.Read()
            If Not IsDBNull(drPendingBooks("TotalPosts")) Then
                hl_Pending.Text = drPendingBooks("TotalPosts")
            End If
        End If
        drPendingBooks.Close()



        'Check if owner of profile has added any books to his guide to enable or disable the delete button.
        Dim cmdhasGuide As New SqlCommand("SELECT * FROM Guides WHERE GuideCreator = @f1", conn)
        cmdhasGuide.CommandType = CommandType.Text
        cmdhasGuide.Parameters.AddWithValue("@f1", Request.Params("Bibliophile"))

        Dim drhasGuide As SqlDataReader = cmdhasGuide.ExecuteReader()
        If drhasGuide.HasRows = True Then
            btn_DeleteGuide.Enabled = True
        Else
            btn_DeleteGuide.Enabled = False
        End If
        drhasGuide.Close()

        'Checking if owner of profile has added any coding language
        Dim LanguageSql As String = "SELECT * FROM Profile_CodingLanguage WHERE Bibliophile = @f1"
        Dim cmdLanguage As SqlCommand = New SqlCommand(LanguageSql, conn)
        cmdLanguage.CommandType = CommandType.Text

        cmdLanguage.Parameters.AddWithValue("@f1", Request.Params("Bibliophile"))
        Dim drLanguage As SqlDataReader = cmdLanguage.ExecuteReader()
        If drLanguage.HasRows = True Then
            lbl_Languages.Visible = False
        Else
            lbl_Languages.Visible = True
        End If
        drLanguage.Close()

        If IsPostBack = False Then

            'Not counting activity if a Bibliophile is visiting his own profile
            If User.Identity.Name <> Request.Params("Bibliophile") Then
                'Check if Bibliophile has already viewed this profile
                Dim sqlCheckUser As String = "SELECT COUNT(*) FROM Profiles_Viewed WHERE BibliophileVisitor = @f1 AND ProfileVisited = @f2"
                Dim cmdCheck As SqlCommand = New SqlCommand(sqlCheckUser, conn)
                cmdCheck.CommandType = CommandType.Text
                cmdCheck.Parameters.AddWithValue("@f1", User.Identity.Name)
                cmdCheck.Parameters.AddWithValue("@f2", Request.Params("Bibliophile"))
                Dim userCount As Integer = cmdCheck.ExecuteScalar()
                If (userCount = 0) Then
                    'Counting the activity that the user has viewed this particular profile since user has not visited the profile
                    Dim ProfilesViewedSql As String = "INSERT INTO Profiles_Viewed (BibliophileVisitor, ProfileVisited) VALUES (@f1, @f2)"
                    Dim cmdViewed As SqlCommand = New SqlCommand(ProfilesViewedSql, conn)
                    cmdViewed.CommandType = CommandType.Text
                    cmdViewed.Parameters.AddWithValue("@f1", User.Identity.Name)
                    cmdViewed.Parameters.AddWithValue("@f2", Request.Params("Bibliophile"))
                    cmdViewed.ExecuteNonQuery()
                End If
            End If

            'Count Page Activity
            Dim cmdPageActivity As New SqlCommand("INSERT INTO Site_Activity(Activity) VALUES (@f1)", conn)
            cmdPageActivity.CommandType = CommandType.Text
            cmdPageActivity.Parameters.AddWithValue("@f1", "Members")
            cmdPageActivity.ExecuteNonQuery()
        End If
        conn.Close()
    End Sub

    Protected Function getDateFrom(ByVal dateStr As Date) As String
        Dim retDate As String = dateStr.ToString("dd MMMM yyyy", CultureInfo.CreateSpecificCulture("en-US"))
        Return retDate

    End Function

    Protected Function getAgeFrom(ByVal dateStr As Date) As String
        Dim retDate As Integer = Convert.ToInt32(dateStr.ToString("yyyy", CultureInfo.CreateSpecificCulture("en-US")))
        Dim currentDate As Integer = DateTime.Now.Year
        Dim age As Integer = currentDate - retDate
        Return CStr(age) & " years old"
    End Function

    Protected Sub btn_Comment_Click(sender As Object, e As EventArgs) Handles btn_Comment.Click
        Dim btn As Button = DirectCast(sender, Button)
        Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("BooksReviewDBConnectionString1").ConnectionString.ToString)
        Dim SqlString As String = "INSERT INTO [Personal_Messages] ([CommentsName], [Title], [BibliophileName], [CommentsBody]) VALUES (@f1, @f2, @f3, @f4)"
        conn.Open()
        Dim cmd As SqlCommand = New SqlCommand(SqlString, conn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f2", tb_Title.Text)
        cmd.Parameters.AddWithValue("@f3", Request.Params("Bibliophile"))
        cmd.Parameters.AddWithValue("@f4", tb_Comment.Text)
        cmd.ExecuteNonQuery()
        conn.Close()
        conn.Dispose()
        cmd.Dispose()
        Comments.GetMessages(Request.Params("Bibliophile"), CommentsLtr)
        tb_Comment.Text = ""
        tb_Title.Text = ""

    End Sub

    Protected Sub btn_DeleteGuide_Click(sender As Object, e As EventArgs) Handles btn_DeleteGuide.Click
        Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("BooksReviewDBConnectionString1").ConnectionString.ToString)
        Dim cmd As New SqlCommand("DELETE FROM Guides WHERE GuideCreator = @f1", conn)
        conn.Open()
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd.ExecuteNonQuery()
        btn_DeleteGuide.Enabled = False
        conn.Close()
        conn.Dispose()
    End Sub

    Protected Sub btn_Guide_Click(sender As Object, e As EventArgs) Handles btn_Guide.Click
        Response.Redirect("ViewGuide.aspx?GuideCreator=" + Request.Params("Bibliophile"))
    End Sub

    Protected Function getBibliophileName() As String
        Return Request.Params("Bibliophile")
    End Function

    Protected Function getTotalPosts(ByVal totalPosts As Integer) As String
        'If user has made 20 posts then he is upgraded to Book Worm
        Dim userName = Request.Params("Bibliophile")
        If totalPosts > 19 Then
            'Adding user to roles as Bibliophile if it is not a Bibliophile already
            If (Roles.IsUserInRole(userName, "bibliophile")) Then
                Roles.RemoveUserFromRole(userName, "bibliophile")
                Roles.AddUserToRole(userName, "bookworm")
                lbl_RoleName.Text = String.Join("", System.Web.Security.Roles.GetRolesForUser(Request.Params("Bibliophile")))
            End If
        End If
        Return totalPosts.ToString

    End Function



End Class